{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 机器人类API"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 目录\n",
    "- [初始化机器人对象](#初始化机器人对象)\n",
    "- [机器人添加设备](#机器人添加设备)\n",
    "- [获取设备对象](#获取设备对象)\n",
    "- [获取设备CAN_ID](#获取设备CAN_ID)\n",
    "- [删除设备](#删除设备)\n",
    "- [获取机器人本地时间](#获取机器人本地时间)\n",
    "- [延时函数](#延时函数)\n",
    "- [机器人使能](#机器人使能)\n",
    "- [机器人失能](#机器人失能)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "from protobot.can_bus import Robot"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 初始化机器人对象"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "robot = Robot()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 机器人添加设备\n",
    "`add_device(name, node_factory, node_id, *node_params)`\n",
    "\n",
    "参数：\n",
    "- name: 设备名\n",
    "- node_factory: 节点工厂类\n",
    "- node_id: 节点CAN_ID\n",
    "- node_params: 节点参数\n",
    "\n",
    "返回值：\n",
    "- 设备对象"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<protobot.can_bus.nodes.motor.Motor at 0x7f8c03a898>"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from protobot.can_bus.nodes import MotorFactory\n",
    "robot.add_device('motor0', factory = MotorFactory(), node_id = 0x10, reduction=-44)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 获取设备对象\n",
    "`device(name)`\n",
    "\n",
    "参数：\n",
    "- name: 设备名\n",
    "\n",
    "返回值：\n",
    "- 设备对象 / None"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "motor = robot.device('motor0')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 获取设备CAN_ID\n",
    "`device_id(name)`\n",
    "\n",
    "参数：\n",
    "- name: 设备名\n",
    "    \n",
    "返回值：\n",
    "- 设备CAN_ID / None"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "16"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "robot.device_id('motor0')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 删除设备\n",
    "`remove_device(name)`\n",
    "\n",
    "参数：\n",
    "- name: 设备名"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "robot.remove_device('motor0')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 获取机器人本地时间\n",
    "`time()`\n",
    "\n",
    "返回值:\n",
    "- 时间(s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "238.70539546012878"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "robot.time()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 延时函数\n",
    "`delay(seconds)`\n",
    "\n",
    "参数：\n",
    "- seconds: 延时时间(s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "robot.delay(1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 机器人使能\n",
    "`enable()`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "robot.enable()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 机器人失能\n",
    "`disable()`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "robot.disable()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "767d51c1340bd893661ea55ea3124f6de3c7a262a8b4abca0554b478b1e2ff90"
  },
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
